package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/*
* 套餐管理相关接口
* */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /*
    * 套餐信息分页查询
    * */
    @Override
    public PageResult findAllSetmealByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.selectAllSetmealByPage(setmealPageQueryDTO);

        //封装PageResult对象
        long total = page.getTotal();
        List<Setmeal> records = page.getResult();
        return new PageResult(total, records);
    }

    /*
    * 新增套餐操作
    * */
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //添加套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);

        //添加套餐菜品
        //主键回显
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && !setmealDishes.isEmpty()) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }

            setmealDishMapper.insertSetmealDishBatch(setmealDishes);
        }
    }

    /*
    * 套餐的批量删除
    * */
    @Override
    public void deleteSetmealBatch(List<Long> ids) {
        //判断改套餐状态是否为启售
        List<Setmeal> setmeals = setmealMapper.selectSetmealByIds(ids);
        for (Setmeal setmeal : setmeals) {
            Integer status = setmeal.getStatus();
            if(status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //删除套餐的基本信息
        setmealMapper.deleteSetmealByIds(ids);

        //删除套餐菜品关系表的信息
        setmealDishMapper.deleteSetmealDishByIds(ids);
    }

    /*
    * 根据id查询套餐信息
    * */
    @Transactional
    @Override
    public SetmealVO findSetmealById(Long id) {
        //查找套餐基本信息
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        //查找套餐菜品关系信息
        List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealDishById(id);

        //封装setmealVO对象
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /*
    * 修改套餐信息
    * */
    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐基本信息
        setmealMapper.updateSetmeal(setmeal);

        //删除菜品信息
        List<Long> list = new ArrayList<>();
        list.add(setmealDTO.getId());
        setmealDishMapper.deleteSetmealDishByIds(list);

        //新增返回的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && !setmealDishes.isEmpty()) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
        }
        setmealDishMapper.insertSetmealDishBatch(setmealDishes);
    }

    /*
    * 修改套餐状态信息
    * */
    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        // 如果要将套餐修改为启售状态，需要检查套餐内菜品状态
        if (StatusConstant.ENABLE.equals(status)) {
            //查询该套餐下包含的菜品
            List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealDishById(id);
            for (SetmealDish setmealDish : setmealDishes) {
                Dish dish = dishMapper.selectDishById(setmealDish.getDishId());
                Integer dishStatus = dish.getStatus();
                if (StatusConstant.DISABLE.equals(dishStatus)) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }

        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        setmeal.setStatus(status);

        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public void updateSetmealStatusBatch(Integer status, List<Long> setmealIds) {
        for (Long setmealId : setmealIds) {
            Setmeal setmeal = setmealMapper.selectSetmealById(setmealId);
            setmeal.setStatus(status);

            setmealMapper.updateSetmeal(setmeal);
        }

    }
}
